package com.sun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sun.issue.*;
import com.sun.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.apache.commons.io.FileUtils;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BuffIO {
    private static final String project_id = "566";
    private static final String assigned_to_id = "7570";
    private static final String priority_id = "2";
    private static final String redmine_userId = "337";
    private static final String redmine_trackerId = "54";

    @Test
    void test1() {
        String filePath = "D:\\中科软信息\\自学\\io\\new3.txt";
        String filePath2 = "D:\\中科软信息\\自学\\io\\new5.txt";
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(filePath));
            bufferedWriter = new BufferedWriter(new FileWriter(filePath2));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                bufferedWriter.write(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    void test2() {
        String filePath = "D:\\中科软信息\\自学\\io\\new3.txt";
        String filePath2 = "D:\\中科软信息\\自学\\io\\new4.txt";
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
            bos = new BufferedOutputStream(new FileOutputStream(new File(filePath2)));
            byte[] buf = new byte[8];
            int len = 0;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    void test3() throws IOException {
        //对象序列化
        String filePath = "D:\\中科软信息\\自学\\io\\data.txt";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
        oos.write(100);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.5);
        oos.writeUTF("张三");
        oos.writeObject(new Dog("猪猪", "10"));

        oos.close();

    }

    @Test
    public void test5() {
        //2021-08-31T01:57:54Z
//        Issue issue = new Issue();
//        List<Issue> issues = new ArrayList<>();
//        issues.add(new Issue())
        String str = "2021-08-31T01:57:54Z";
        String str2 = "2021-08-31T02:57:54Z";
        ZonedDateTime z1 = ZonedDateTime.parse(str);
        ZonedDateTime z2 = ZonedDateTime.parse(str2);
        LocalDateTime l1 = z1.toLocalDateTime();
        LocalDateTime l2 = z2.toLocalDateTime();
        int i = l1.compareTo(l2);
        System.out.println(i);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    }

    @Test
    public void test6() {
        List list = new ArrayList();
//        list.add();
    }

    @Test
    public void test4() throws IOException {
        Issue i1 = get(1);
        Issue i2 = get(2);
        List<Issue> list = new ArrayList();
        list.add(i1);
        list.add(i2);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<Issue> collect = list.stream().sorted(new Comparator<Issue>() {
            @Override
            public int compare(Issue o1, Issue o2) {
                ZonedDateTime z1 = ZonedDateTime.parse(o1.getCreated_on());
                ZonedDateTime z2 = ZonedDateTime.parse(o2.getCreated_on());
                LocalDateTime l1 = z1.toLocalDateTime();
                LocalDateTime l2 = z2.toLocalDateTime();
                int i = l1.compareTo(l2);
                return -i;
            }
        }).collect(Collectors.toList());

//        for (Issue x : collect) {
//            ZonedDateTime zonedDateTime = ZonedDateTime.parse(x.getCreated_on());
//            LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
//            String format = dateTimeFormatter.format(localDateTime.plusHours(8));
//            x.setCreated_on(format);
//            System.out.println(x);
//        }
        collect.stream().forEach(x -> {
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(x.getCreated_on());
            LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
            String format = dateTimeFormatter.format(localDateTime.plusHours(8));
            x.setCreated_on(format);
        });
        System.out.println(collect);
    }

    public Issue get(int sum) {
        Issue issue = new Issue();
        issue.setPriority_id(project_id);
        issue.setAssigned_to_id(assigned_to_id);
        issue.setPriority_id(priority_id);
        List<CustomerFields> list = new ArrayList<>();
        list.add(new CustomerFields("51", "模块", "23-投保app"));
        list.add(new CustomerFields("56", "问题类型", "1-程序BUG"));
        list.add(new CustomerFields("4", "OA申请编号", "无"));
        list.add(new CustomerFields(redmine_userId, "用户-事件提出人", "156153"));
        issue.setCustom_fields(list);
        issue.setTracker(new Tracker(redmine_trackerId, "工单运维"));
        issue.setId("145612313");
        issue.setSubject("subject");
        issue.setDescription("description");
        List<Upload> uploads = new ArrayList<>();
        uploads.add(new Upload("1561231", "dsa5d4213546", "filename.png", "image/png"));
        issue.setUploads(uploads);
        issue.setDone_ratio(0);
        if (sum == 1) {
            issue.setCreated_on("2021-08-31T01:57:54Z");
        } else {
            issue.setCreated_on("2021-08-31T01:58:54Z");
        }
        return issue;
    }

    public static void main(String[] args) throws ParseException {
        String getUrl = "http://it.abchinalife.cn/redmine/issues/?key=babcf4979cf692e1bf65c1ef618f0b17506de5e2&include=journals";
        int index = getUrl.indexOf("?");
        String id = "123";
        String url = getUrl.substring(0, index) + id + ".json" + getUrl.substring(index, getUrl.length());
        System.out.println(url);
    }

    @Test
    public void test06() throws Exception {
        FileInputStream fis = new FileInputStream(new File("D:\\details.txt"));
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        int length = 0;
        while ((length = fis.read(buf)) != -1) {
            sb.append(new String(buf, 0, length));
        }
        System.out.println(sb);
    }

    @Test
    public void test07() {
        File file = new File("C:\\Users\\sun\\Desktop\\total");
        for (File listFile : file.listFiles()) {
            System.out.println(listFile.getName());
            try (FileInputStream fis = new FileInputStream(listFile)) {
                byte[] buf = new byte[1024];
                StringBuilder sb = new StringBuilder();
                int length = 0;
                while ((length = fis.read(buf)) != -1) {
                    sb.append(new String(buf, 0, length));
                }
                List<Map<String, String>> list = JSON.parseObject(sb.toString(), new TypeReference<List<Map<String, String>>>() {
                });
                for (Map<String, String> map : list) {
                    String userCode = map.get("userId").split("-")[2];
                    String chatSum = map.get("chatSum");
                    String sql = "update lsTotal set sum = sum + " + chatSum + " where usercode = '" + userCode + "';";
                    System.out.println(sql);
                }
            } catch (Exception e) {

            }
        }

    }

    @Test
    public void test08() throws Exception{
        File file = new File("D:\\中科软信息\\t1\\t2\\1.txt");
        if (!file.exists()){
            FileUtils.forceMkdirParent(file);
        }else {
            System.out.println(11);
        }

    }
}
